package com.ityuwen.mengyagushi.order.service.template;

import com.ityuwen.mengyagushi.common.constant.SystemConstant;
import com.ityuwen.mengyagushi.common.result.Result;
import com.ityuwen.mengyagushi.model.user.VipServiceConfig;
import com.ityuwen.mengyagushi.user.client.VipServiceConfigFeignClient;
import com.ityuwen.mengyagushi.vo.order.OrderDerateVo;
import com.ityuwen.mengyagushi.vo.order.OrderDetailVo;
import com.ityuwen.mengyagushi.vo.order.OrderInfoVo;
import com.ityuwen.mengyagushi.vo.order.TradeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@OrderType(OrderConfirmType.ORDER_CONFIRM_VIP)
public class VipOrderConfirm extends OrderConfirm {

    @Autowired
    private VipServiceConfigFeignClient vipServiceConfigFeignClient;

    @Autowired
    public VipOrderConfirm(RedisTemplate redisTemplate) {
        super(redisTemplate);
    }

    @Override
    public void confirm(TradeVo tradeVo, OrderInfoVo orderInfoVo) {
        // 获取vip套餐
        Result<VipServiceConfig> vipServiceConfigResult = this.vipServiceConfigFeignClient.getVipServiceConfig(tradeVo.getItemId());
        Assert.notNull(vipServiceConfigResult, "vip订单确认页：获取vip套餐失败！");
        VipServiceConfig vipServiceConfig = vipServiceConfigResult.getData();
        Assert.notNull(vipServiceConfig, "vip订单确认页：获取vip套餐失败！");

        // 设置金额信息
        BigDecimal originAmount = vipServiceConfig.getPrice();
        BigDecimal orderAmount = vipServiceConfig.getDiscountPrice();
        BigDecimal derateAmount = originAmount.subtract(orderAmount);
        orderInfoVo.setOriginalAmount(originAmount);
        orderInfoVo.setDerateAmount(derateAmount);
        orderInfoVo.setOrderAmount(orderAmount);

        // 订单详情信息：vip订单只有一个
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVoList.add(orderDetailVo);
        orderDetailVo.setItemId(tradeVo.getItemId());
        orderDetailVo.setItemName("vip购买：" + vipServiceConfig.getName());
        orderDetailVo.setItemPrice(orderAmount);
        orderDetailVo.setItemUrl(vipServiceConfig.getImageUrl());
        orderInfoVo.setOrderDetailVoList(orderDetailVoList);

        // 订单减免信息：现在就只有一个
        List<OrderDerateVo> orderDerateVoList = new ArrayList<>();
        OrderDerateVo orderDerateVo = new OrderDerateVo();
        orderDerateVoList.add(orderDerateVo);
        orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_VIP_SERVICE_DISCOUNT);
        orderDerateVo.setDerateAmount(derateAmount);
        orderInfoVo.setOrderDerateVoList(orderDerateVoList);
    }
}
